import {
    Component,
    Injector,
    ViewEncapsulation,
    ViewChild,
    ElementRef,
    OnInit,
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    NodeWorkerForPipleinOutput,
    UserWorkingTimeServiceProxy,
    PipelineNodeServiceProxy,
} from '@shared/service-proxies/service-proxies';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import * as moment from 'moment';
import { finalize } from 'rxjs/operators';
import { CreateOrEditTaskworkingModalComponent } from './create-or-edit-taskworking-modal.component';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';

@Component({
    templateUrl: './taskworking.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()],
})
export class TaskworkingComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    public startDate: moment.Moment = moment().startOf("day");
    public endDate: moment.Moment = moment().endOf("day");
    advancedFiltersAreShown = false;
    work: NodeWorkerForPipleinOutput[];
    en: any;
    filterText = '';
    zde=-1;

    constructor(
        injector: Injector,
        private _fileDownloadService: FileDownloadService,
        private _userWorkingTimeServiceProxy: UserWorkingTimeServiceProxy
    ) {
        super(injector);
    }

    ngOnInit() {
        // this.en = {
        //   firstDayOfWeek: 0,
        //   monthNames: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        //   monthNamesShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        //   dateFormat: 'yy-mm',
        //   weekHeader: 'Wk'
        // };
    }
    getWorking(event?: LazyLoadEvent) {
        this.primengTableHelper.showLoadingIndicator();
        // let start = null;
        // let end = null;
        // console.log(this.dateRange);
        // if (this.dateRange !== undefined) {
        //     start = moment(this.dateRange[0]);
        //     end = moment(this.dateRange[1]);
        // }
        if(this.zde==null)
        {
            this.zde=-1;
        }
        this._userWorkingTimeServiceProxy
            .getPipelineWorkers(
                this.startDate,
                this.endDate,
                this.filterText,
                this.zde,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(
                    this.paginator,
                    event
                ),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe((result) => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    remWorking() {
       // this.dateRange = undefined;
        this.getWorking();
    }
    deleteModel(id: string) {
        this.message.confirm('确认删除该条数据以及数据下的人员分配工时吗？', (isConfirmed) => {
            if (isConfirmed) {
                this._userWorkingTimeServiceProxy
                    .deleteProc(id)
                    .subscribe(() => {
                        this.notify.success(this.l('SuccessfullyDeleted'));
                        this.getWorking();
                    });
            }
        });
    }
    clean() {
        //this.dateRange = undefined;
        this.getWorking();
    }
    exportToExcel(): void {
        // let start = null;
        // let end = null;
        // if (this.dateRange !== undefined) {
        //     start = moment(this.dateRange[0]);
        //     end = moment(this.dateRange[1]);
        // }
        this._userWorkingTimeServiceProxy
            .getPipelineWorkersToExcel(this.startDate,
                this.endDate, this.filterText)
            .subscribe((result) => {
                this._fileDownloadService.downloadTempFile(result);
            });
    }
}
